package com.ruoyi.web.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.web.domain.DebtsSection;
import com.ruoyi.web.domain.DebtsSectionAttachment;
import com.ruoyi.web.domain.dto.DebtsSectionDto;
import com.ruoyi.web.domain.vo.DebtsSectionVo;
import com.ruoyi.web.mapper.DebtsSectionMapper;
import com.ruoyi.web.service.DebtsSectionAttachmentService;
import com.ruoyi.web.service.DebtsSectionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class DebtsSectionServiceImpl extends ServiceImpl<DebtsSectionMapper, DebtsSection> implements DebtsSectionService {

    @Autowired
    private DebtsSectionMapper debtsSectionMapper;

    @Autowired
    private DebtsSectionAttachmentService debtsSectionAttachmentService;

    @Transactional(rollbackFor = Throwable.class)
    @Override
    public void addDebts(DebtsSectionDto dto) {
        DebtsSection debtsSection = new DebtsSection();
        BeanUtils.copyProperties(dto, debtsSection);
        Long userId = SecurityUtils.getLoginUser().getUserId();
        debtsSection.setCreateBy(userId);
        debtsSection.setCreateTime(LocalDateTime.now());
        List<DebtsSection> debtsList = this.list(new LambdaQueryWrapper<DebtsSection>().orderByDesc(DebtsSection::getBatchNumber));
        int batchNumber = 1;
        if (debtsList.size() > 0) {
            batchNumber = debtsList.get(0).getBatchNumber() + 1;
        }
        debtsSection.setBatchNumber(batchNumber);
        // 插入清欠数据
        debtsSectionMapper.insert(debtsSection);

        List<DebtsSectionAttachment> attachments = dto.getAttachments();
        if (attachments != null && attachments.size() > 0) {
            // 对附件文件进行存储
            List<DebtsSectionAttachment> attachmentList = attachments.stream().peek(i -> i.setDebtsId(debtsSection.getBaseId())).collect(Collectors.toList());
            debtsSectionAttachmentService.saveBatch(attachmentList);
        }
    }

    @Override
    public List<DebtsSectionVo> getList(DebtsSectionDto dto) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if (StrUtil.equals(user.getRemark(), "市级账号")) {
            // 市级账号只能查看到指派给自己的清欠数据
            dto.setDealerDeptId(user.getDeptId());
        }
        return debtsSectionMapper.selectDebtsList(dto);
    }

    @Override
    public DebtsSectionVo getDetails(Long baseId) {
        DebtsSectionVo debtsSectionVo = debtsSectionMapper.selectDebtsDetails(baseId);
        List<DebtsSectionAttachment> attachmentList = debtsSectionAttachmentService.list(new LambdaQueryWrapper<DebtsSectionAttachment>().eq(DebtsSectionAttachment::getDebtsId, baseId).orderByAsc(DebtsSectionAttachment::getSort));
        debtsSectionVo.setAttachments(attachmentList);
        return debtsSectionVo;
    }

    @Override
    public void dispatchDebts(DebtsSectionDto dto) {
        debtsSectionMapper.update(new LambdaUpdateWrapper<DebtsSection>().eq(DebtsSection::getBaseId, dto.getBaseId())
                .set(DebtsSection::getDealerDeptId, dto.getDealerDeptId())
                .set(DebtsSection::getDealerDeptName, dto.getDealerDeptName())
                .set(DebtsSection::getDispatchTime, LocalDateTime.now())
                .set(DebtsSection::getStatus, 1));
    }

    @Override
    public void dealDebts(DebtsSectionDto dto) {
        debtsSectionMapper.update(new LambdaUpdateWrapper<DebtsSection>().eq(DebtsSection::getBaseId, dto.getBaseId())
                .set(DebtsSection::getDealMethod, dto.getDealMethod())
                .set(DebtsSection::getProblemReason, dto.getProblemReason())
                .set(DebtsSection::getSummaryExperience, dto.getSummaryExperience())
                .set(DebtsSection::getCompleteTime, LocalDateTime.now())
                .set(DebtsSection::getStatus, 2));
    }
}
